package com.duke.shaking.widget;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.androidquery.AQuery;
import com.duke.shaking.Constants;
import com.duke.shaking.R;
import com.duke.shaking.pojo.UserImage;
import com.duke.shaking.utils.DeviceUtils;

public class PhotoMgrGridAdapter extends BaseAdapter {
	
	private static final String TAG = "PhotoMgrGridAdapter";

	private LayoutInflater inflater;
	private List<UserImage> photos;
	private AQuery aQuery;
	private Map<Integer, Boolean> refMap;
	private boolean editable;
	private int colWidth;
	
	public PhotoMgrGridAdapter(Context context, List<UserImage> photos) {
		this.photos = photos;
		inflater = LayoutInflater.from(context); 
		aQuery = new AQuery(context);
		this.editable = true;
		calculateColumnWidth();
	}
	
	private void calculateColumnWidth() {
		int width = DeviceUtils.getDisplayWidthPix();
		this.colWidth = (width - DeviceUtils.dip2px(10 * 2) - 10 * 3) / 4;
	}
	
	public void setEditable(boolean editable) {
		this.editable = editable;
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		int count = 0;
		if (photos != null) {
			count = photos.size();
		}
		if (count < Constants.maxPhotosCount) {
			return count + (this.editable ? 1 : 0);
		}
		else {
			return Constants.maxPhotosCount;
		}
	}

	public Object getItem(int position) {
		if (photos != null && position < photos.size()) {
			return photos.get(position);
		}
		return null;
	}

	public long getItemId(int position) {
		
		return position;
	}

	public View getView(int position, View contentView, ViewGroup parent) {
		PhotoViewHolder holder = null;
		if (contentView == null) {
			contentView = inflater.inflate(R.layout.view_photomgr_listitem, null);
			holder = new PhotoViewHolder();
			holder.img_photo = (ImageView)contentView.findViewById(R.id.img_photo);
			contentView.setTag(holder);
			
			// change column width
			LayoutParams layoutParams = new LayoutParams(colWidth, colWidth);
			holder.img_photo.setLayoutParams(layoutParams);
		}
		else {
			holder = (PhotoViewHolder)contentView.getTag();
		}

		UserImage img = (UserImage)getItem(position);
		if (position + 1 == getCount() && photos.size() < Constants.maxPhotosCount) {
			//aQuery.id(holder.img_photo).image(R.drawable.upload_photo);
			holder.img_photo.setImageResource(R.drawable.upload_photo);
			holder.img_photo.setScaleType(ScaleType.CENTER_INSIDE);
		}
		else if(img == null) {
			holder.img_photo.setImageResource(R.drawable.blackgrey_bg);
			//aQuery.id(holder.img_photo).image(R.drawable.blackgrey_bg);
			holder.img_photo.setScaleType(ScaleType.FIT_XY);
		} else {
			//Log.i(TAG, "No." + position + " url:" + img.getUrl_1());
			holder.img_photo.setImageResource(R.drawable.blackgrey_bg);
			aQuery.id(holder.img_photo).image(img.getUrl_1(), true, true, 0, R.drawable.blackgrey_bg);
			holder.img_photo.setScaleType(ScaleType.CENTER_CROP);
		}

		return contentView;
	}

	private class PhotoViewHolder {
		public ImageView img_photo;
	}
}
